#pragma once
#include <iostream>
#include <unistd.h>
#include <pthread.h>
#include <vector>
#include "sem.hpp"

using namespace SemModule;

const int BufferSize = 5;

template<class T>
class RingQueue{
public:
	RingQueue():_rq(BufferSize),
	_cap(BufferSize),
	_blank_sem(BufferSize),
	_pstep(0),
	_data_sem(0),
	_cstep(0){}

	void Equeue(const T& input){
		_blank_sem.P();
		_rq[_pstep] = input;
		_pstep++;
		_pstep %= _cap;
		_data_sem.V();
	}

	void Pop(T& output){
		_data_sem.P();
		output = _rq[_cstep];
		_cstep++;
		_cstep %= _cap;
		_blank_sem.V();
	}

	~RingQueue() = default;

private:
	std::vector<T> _rq;
	int _cap;

	//生产者
	Sem _blank_sem;
	int _pstep;

	//消费者
	Sem _data_sem;
	int _cstep;
};
